package club.vann.kafka.mock.service.quote;

import club.vann.kafka.mock.kafka.KafkaProducer;
import club.vann.kafka.mock.util.AcceptorUtil;
import com.cyberx.lunyu.AcceptorOuterClass;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

@Component
public class QuoteConfirmService extends QuoteAcceptorService {

  protected QuoteConfirmService(KafkaProducer kafkaProducer) {
    super(kafkaProducer);
  }

  @Override
  public AcceptorOuterClass.Acceptor.RequestType requestType() {
    return AcceptorOuterClass.Acceptor.RequestType.QUOTE_CONFIRM;
  }

  @Override
  public void input(AcceptorOuterClass.Acceptor.AcceptorRequest request) {
    String clientRequestId = request.getClientRequestId();
    String responseTopic = request.getTopic();
    Map<String, String> dataMap = request.getDataMap();
    String id = dataMap.get("ID");
    if (ObjectUtils.isEmpty(QUOTE_INFO_MAP.get(id))) {
      return;
    }
    Map<String, String> requestMap = QUOTE_INFO_MAP.get(id);

    Map<String, String> responseMap = new HashMap<>();
    responseMap.put("TICKER", requestMap.get("TICKER"));
    responseMap.put("STATE", "COMPLETED");
    responseMap.put("SIDE", requestMap.get("SIDE"));
    responseMap.put("AVG_FILLED_PRICE", "65200.67");
    responseMap.put("FILLED_QTY", "0.2");
    responseMap.put("ID", requestMap.get("ID"));
    responseMap.put("TOTAL_FEE", "20");
    responseMap.put("CLIENT_ID", requestMap.get("CLIENT_ID"));

    AcceptorOuterClass.Acceptor.AcceptorResponse acceptorResponse =
        AcceptorUtil.ofResponse(
            clientRequestId,
            AcceptorOuterClass.Acceptor.Status.COMPLETED,
            responseTopic,
            "",
            responseMap);
    this.kafkaProducer.send(responseTopic, clientRequestId, acceptorResponse.toByteArray());
  }
}
